﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using CCPnuts.ISkyDiskFileSystem;
using System.Threading;
using System.Collections;
using HgCo.WindowsLive.SkyDrive;

namespace TestSky
{
    class Program
    {
        static void Main(string[] args)
        {
            // ! This is just a test. It doesn't connect to the SkyDevice Server. 

            //initial
            TestSkyDiskFileSystem.Initial.Init();

            // The Username & Password is any string, cause this is just a test.
            var disk = SkyDisk.Login("lily@lily.bbs", "lilystudio");

            Console.WriteLine("Login Success.");

            //Show the items of the disk.
            Console.WriteLine("Print any key to show the items of the root disk.");
            Console.ReadKey(true);
            Dir(disk);

            #region How to get items of a container

            //Get the sub directory of parent container.
            SkyElementContainer subDirectory = disk.GetDir("cjypdkm");
            //Or 
            subDirectory = disk["cjypdkm"];
            Dir(subDirectory);

            //Get the sub file of parent container
            SkyFile subFile = disk.GetFile("okt");
            //Or
            //The "." means the disk itself. You also can use ".." represent the parent.
            subFile = disk[".", "okt"];
            //Or
            //You can also write like this if you like.
            subFile = disk["cjypdkm/./../", "okt"];

            #endregion

            #region How to download a file

            Console.WriteLine("Press any key to begin a download.");
            Console.ReadKey(true);

            //Get a downloader before download.
            Downloader down = subFile.Download("Testpath");

            //Set the update event.
            down.TransmissionUpdate += transmitor =>
            {
                Console.WriteLine(
                    String.Format("Peresent : {0}%  Speed : {1} byte/s",
                        transmitor.Sent * 100.0 / transmitor.TotalToSend, 
                        transmitor.Speed));
            };

            down.Run();

            //You can also use the BeginRun() & EndRun();

            down = subFile.Download("Testpath");

            var ia = down.BeginRun(e => { Console.WriteLine("Download Over"); });

            down.EndRun(ia);

            #endregion

            #region How to upload a file

            //Just like how you download a file. 
            //I'm lazy....
            //You can see the Upload Function in SkyElementContainer

            #endregion

            #region Others

            //I thought, I have already said that I'm lazy ...

            #endregion
        }

        static void Dir(SkyElementContainer container)
        {
            foreach (var i in container.Children)
            {
                string type = ("[" + i.ElementType + "]     ").Substring(0, 11);
                Console.WriteLine(String.Format(" {0}    {1}", type, i.Name));
            }
        }

        static void Output(int level, SkyElementContainer c)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < level; i++)
            {
                sb.Append("  ");
            }
            sb.Append("# ");
            foreach (var e in c.Children)
            {
                Console.Write(sb.ToString());
                Console.WriteLine(e.Name);
                if (e is SkyElementContainer)
                {
                    Output(level + 1, e as SkyElementContainer);
                }
            }
        }
    }

}
